package com.huike.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.huike.business.domain.BusinessTrackRecord;
import com.huike.business.domain.BusinessTrackRecordDto;
import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.clues.service.ISysDictDataService;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.service.ITbBusinessTrackRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 *
 * @author wgl
 *
 * @author ruoyi
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {
    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;

    @Autowired
    private TbBusinessMapper tbBusinessMapper;



    /**
     * 查询商机跟进记录列表
     *
     * @param tbBusinessTrackRecord 商机跟进记录
     * @return 商机跟进记录
     */
    @Override
    public List<TbBusinessTrackRecord> selectTbBusinessTrackRecordList(TbBusinessTrackRecord tbBusinessTrackRecord){
        return tbBusinessTrackRecordMapper.selectTbBusinessTrackRecordList(tbBusinessTrackRecord);
    }


    /**
     * 修改商机跟进记录
     *
     * @param tbBusinessTrackRecord 商机跟进记录
     * @return 结果
     */
    @Override
    public int updateTbBusinessTrackRecord(TbBusinessTrackRecord tbBusinessTrackRecord)
    {
        return tbBusinessTrackRecordMapper.updateTbBusinessTrackRecord(tbBusinessTrackRecord);
    }

    /**
     * 批量删除商机跟进记录
     *
     * @param ids 需要删除的商机跟进记录ID
     * @return 结果
     */
    @Override
    public int deleteTbBusinessTrackRecordByIds(Long[] ids)
    {
        return tbBusinessTrackRecordMapper.deleteTbBusinessTrackRecordByIds(ids);
    }

    /**
     * 删除商机跟进记录信息
     *
     * @param id 商机跟进记录ID
     * @return 结果
     */
    @Override
    public int deleteTbBusinessTrackRecordById(Long id)
    {
        return tbBusinessTrackRecordMapper.deleteTbBusinessTrackRecordById(id);
    }

    /**
     * 新增商机跟进记录
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(BusinessTrackVo businessTrackVo) {
        // 新增商机跟进记录
        BusinessTrackRecord businessTrackRecord = new BusinessTrackRecord();
        BeanUtils.copyProperties(businessTrackVo, businessTrackRecord);
        businessTrackRecord.setBusinessId(businessTrackVo.getBusinessId().toString());
        businessTrackRecord.setCreateBy(SecurityUtils.getUsername());
        businessTrackRecord.setCreateTime(new Date());

        tbBusinessTrackRecordMapper.add(businessTrackRecord);

        // 修改商机数据
        TbBusiness tbBusiness = new TbBusiness();
        BeanUtils.copyProperties(businessTrackVo, tbBusiness);
        tbBusiness.setStatus("2");
        tbBusiness.setId(businessTrackVo.getBusinessId());

        tbBusinessMapper.updateTbBusiness(tbBusiness);
    }

    /**
     * 商机跟进记录列表
     * @param businessId
     * @return
     */
    // @Override
    // public List<BusinessTrackRecordDto> list(Long businessId) {
    //     List<BusinessTrackRecordDto> businessTrackRecordDtos = tbBusinessTrackRecordMapper.list(businessId);
    //
    //     if (businessTrackRecordDtos == null || businessTrackRecordDtos.size() < 1) {
    //         throw new RuntimeException("暂无数据");
    //     }
    //
    //     ArrayList arrayList = new ArrayList<>();
    //     arrayList.add("课程");
    //     arrayList.add("价格");
    //     arrayList.add("位置");
    //     arrayList.add("时间");
    //     arrayList.add("师资");
    //     arrayList.add("项目");
    //     arrayList.add("薪资");
    //     arrayList.add("职业");
    //
    //     for (BusinessTrackRecordDto businessTrackRecordDto : businessTrackRecordDtos) {
    //         String[] split = businessTrackRecordDto.getKeyItems().split(",");
    //         if (split == null || split.length < 1) {
    //             continue;
    //         }
    //         ArrayList<String> list = new ArrayList<>();
    //         for (String s : split) {
    //             Integer index = Integer.valueOf(s);
    //             String str = (String) arrayList.get(index);
    //             list.add(str);
    //         }
    //         businessTrackRecordDto.setKeys(list);
    //     }
    //     return businessTrackRecordDtos;
    // }

    /**
     * 查询商机跟进记录列表
     *
     * @param id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> findByliebiao(Long id) {

        //获取商机跟进记录列表中测所有数据
        List<TbBusinessTrackRecord> list = tbBusinessTrackRecordMapper.findBylirbiao(id);

        if (list.isEmpty()) {
            for (TbBusinessTrackRecord tbBusiness : list) {
                //获取keyItems中的字符串
                String keyItems = tbBusiness.getKeyItems();
                //用","来分割字符串，把获得的新字符串用一个新的数组来接收
                String[] split = keyItems.split(",");
                //遍历字符串数组，把keyItems中的数组对应的数字用来筛选keys对应的字段
                for (String s : split) {
                    //调用mapper方法，根据split中 s 来获取相对应的字段
                    Integer ids = Integer.valueOf(s);
                    String key = tbBusinessTrackRecordMapper.fianById(ids);
                    //设置相对应的字段值
                    tbBusiness.getKeys().add(key);
                }
            }

        }

        return list;
    }
}
